<template>
  <!-- 牌组类型 -->
  <el-dialog :visible.sync="isShow" v-if="isShow" title="牌组类型" width="50%" :before-close="handleClose" top="100px" :close-on-click-modal="false">
    <div class="border-box">
      <el-form ref="deckForm" :model="formParams" :rules="ruleForm" label-position="right" label-width="0">
        <div class="flex item-box" v-for="(item,index) in itemData" :key="index">
          <div class="title width25">
            {{ item.label }}<span :class="item.pass?'mustPass':'noMustPass'">*</span>
          </div>
          <div class="content width80">
            <el-form-item class="item-clear" v-show="item.key=='game_id'" label="" prop="game_id">
              <el-select v-model="formParams.game_id" placeholder="请选择游戏类型" filterable disabled>
                <el-option :label="second.label" :value="second.value" v-for="(second, two) in GAME_TYPE" :key="two"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_base_arrange_id'" label="" prop="a_base_arrange_id">
              <el-input-number :min="0" :precision="0" v-model="formParams.a_base_arrange_id" controls-position="right" clearable placeholder="请输入牌型id" />
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_arrange_remark'" label="" prop="a_arrange_remark">
              <el-input class="width40" type="text" v-model="formParams.a_arrange_remark" clearable placeholder="请输入牌型备注" />
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='weight'" label="" prop="weight">
              <el-input-number :min="0" :max="999999999" :precision="0" v-model="formParams.weight" controls-position="right" clearable placeholder="请输入牌型权重"/>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_arrange_content'" label="" prop="a_arrange_content" style="height:auto;padding-bottom:5px">
              <el-select v-model="formParams.a_arrange_content" placeholder="请选择配置内容" filterable multiple style="width:100%">
                <el-option :label="cardType(second.type)+second.label" :value="second.value" v-for="(second, two) in cardDataList" :key="two"></el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('deckForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addDeck,
  editDeck
} from '@/api/game'
import {
  GAME_TYPE
} from '@/utils/enum'
export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
    cardData: {
      type: Array,
      default: function () {
        return []
      }
    },
    gameType: {
      type: Number,
      default: 1
    },
  },
  data () {
    return {
      GAME_TYPE,
      isShow: false,
      itemData:[
        { label: '游戏类型', key: 'game_id' },
        { label: '牌型ID', key: 'a_base_arrange_id' ,pass:true},
        { label: '牌型备注', key: 'a_arrange_remark' ,pass:true},
        { label: '配置内容', key: 'a_arrange_content' ,pass:true},
        { label: '牌型权重', key: 'weight' ,pass:true},
      ],
      formParams: {
        game_id:this.gameType,
        a_base_arrange_id: '', //牌型id
        a_arrange_remark: '', //备注
        a_arrange_content: '', //配置内容
        weight:''
      },
      ruleForm: {
        a_base_arrange_id: [{ required: true, trigger: 'blur', message: '请输入牌型id', }],
        a_arrange_remark: [{ required: true, trigger: 'blur', message: '请输入备注', }],
        weight: [{ required: true, trigger: 'blur', message: '请输入牌型权重', }],
        a_arrange_content: [{ required: true, trigger: 'blur', message: '请选择配置内容', }],
      },
      cardDataList: [],
      cardTypeData:[{
        label:'方块',
        value:1
      },{
        label:'梅花',
        value:2
      },{
        label:'红桃',
        value:3
      },{
        label:'黑桃',
        value:4
      }]
    }
  },
  computed: {},
  watch: {
    show: function () {
      let that = this,typeData=[];
      that.isShow = that.show
      if (that.data) {
        that.formParams = JSON.parse(JSON.stringify(that.data))
        typeData=that.data.a_arrange_content.split(',')
        that.formParams.a_arrange_content=[]
        typeData.map(item=>{
          that.formParams.a_arrange_content.push(Number(item))
        })
      } else {
        that.formParams = {
          game_id:that.gameType,
          a_base_arrange_id: '', //牌型id
          a_arrange_remark: '', //备注
          a_arrange_content: '', //配置内容
          weight:''
        }
      }
    },
    cardData:{
      handler(newVal, oldVal) {
        let that=this;
        if(newVal){
          that.cardDataList=newVal
        }
      },
      deep: true,
      immediate: true,
    }
  },
  mounted () {},
  methods: {
    // 确认
    handleConfirm (formvalue) {
      let that = this, params = JSON.parse(JSON.stringify(that.formParams));
      params.a_arrange_content=params.a_arrange_content.toString()
      that.$refs[formvalue].validate((valid) => {
        if (valid) {
          // 新增
          if (!params.id) {
            addDeck(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          } else {
            editDeck(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }
        } else {
          return false;
        }
      });
    },
    // 基础牌展示
    cardType(cellValue){
      return (this.cardTypeData.find((item) => item.value == cellValue)?.label || "-");
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
.encryption {
  height: 10px;
  line-height: 10px;
  margin-bottom: -10px;
  font-size: 12px;
  color: darkred;
}
</style>
